package com.defangit.applicant.orm;

import com.defangit.applicant.domain.Enroll;
import laogen.hibernate.CommonFinder;
import laogen.hibernate.CommonService;
import laogen.lang.exception.ParameterNotValidException;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Administrator on 2019/3/9.
 */
public class EnrollService extends CommonService<Enroll>{
    private static Logger logger= LoggerFactory.getLogger(EnrollService.class);


    //添加报名活动
    public Enroll addEnroll(Enroll enroll){
        if(enroll ==null) throw ParameterNotValidException.zeroIdOrEntityNonExist("要添加的报名活动");
        if (enroll.getEnrollName()==null||enroll.getOrgId()==0) throw ParameterNotValidException.zeroIdOrEntityNonExist("活动名称/发起人");
        add(enroll);
        commitSession();
        return enroll;
    }
    //要删除的报名活动
    public boolean delEnroll(int id){
        Enroll enroll =get(id)
                .orElseThrow(()->ParameterNotValidException.zeroIdOrEntityNonExist("要删除的报名活动id"));
        delete(enroll);
        commitSession();
        return true;
    }
    //修改报名活动
    public Enroll updateEnroll(Enroll enroll){
        if(enroll==null)throw ParameterNotValidException.zeroIdOrEntityNonExist("要修改的报名活动");
        if (enroll.getOrgId()==0||enroll.getOrgId()==0)throw ParameterNotValidException.zeroIdOrEntityNonExist("");
        update(enroll);
        commitSession();
        return enroll;
    }

    public EnrollFinder findAll(){
        return new EnrollFinder();
    }

    public class EnrollFinder extends CommonFinder<Enroll>{
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Enroll.class);

        @Override //为Enroll实体类提供criteria查询方法
        public Criteria getCriteria() {
            return detachedCriteria.getExecutableCriteria(getSession());
        }

        @Override   //为Enroll实体类提供detachedcriteria查询方法
        public DetachedCriteria getDetachedCriteria() {
            return detachedCriteria;
        }

        public EnrollFinder enrollId(Integer enrollId){
            if (enrollId!=null&&enrollId!=0)
                detachedCriteria = detachedCriteria.add(Restrictions.eq("id",enrollId));
            return this;
        }
        public EnrollFinder ticketype(Integer ticketTypeId){//创建联合查询,Enroll实体类服务提供ticket实体属性Id查询结果
            if (ticketTypeId!=null&&ticketTypeId!=0)
                detachedCriteria = detachedCriteria.createAlias("ticketTypes","ticketType").add(Restrictions.eq("ticketType.id",ticketTypeId));
            return this;
        }
        public EnrollFinder orgid(Integer orgId){
            if (orgId!=null&&orgId!=0)
                detachedCriteria=detachedCriteria.add(Restrictions.eq("orgId",orgId));
             return this;
        }

    }
}
